package com.wheat.vipcard.bean;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.LockMode;
import org.hibernate.Query;
import org.hibernate.criterion.Example;

import com.wheat.vipcard.utils.PageResultSet;
import com.wheat.vipcard.utils.PageUtil;

/**
 * A data access object (DAO) providing persistence and search support for Zone
 * entities. Transaction control of the save(), update() and delete() operations
 * can directly support Spring container-managed transactions or they can be
 * augmented to handle user-managed Spring transactions. Each of these methods
 * provides additional information for how to configure it for the desired type
 * of transaction control.
 * 
 * @see com.wheat.vipcard.bean.Zone
 * @author MyEclipse Persistence Tools
 */

public class ZoneDAO extends BaseHibernateDAO {
	private static final Log LOG = LogFactory.getLog(ZoneDAO.class);
	// property constants
	public static final String ZONE_CODE = "zoneCode";
	public static final String ZONE_NAME = "zoneName";
	public static final String ZONE_OF_CODE = "zoneOfCode";

	public void save(Zone transientInstance) {
		LOG.debug("saving Zone instance");
		try {
			getSession().save(transientInstance);
			LOG.debug("save successful");
		} catch (RuntimeException re) {
			LOG.error("save failed", re);
			throw re;
		}
	}

	public void delete(Zone persistentInstance) {
		LOG.debug("deleting Zone instance");
		try {
			getSession().delete(persistentInstance);
			LOG.debug("delete successful");
		} catch (RuntimeException re) {
			LOG.error("delete failed", re);
			throw re;
		}
	}

	public Zone findById(java.lang.Integer id) {
		LOG.debug("getting Zone instance with id: " + id);
		try {
			Zone instance = (Zone) getSession().get("com.wheat.vipcard.bean.Zone", id);
			return instance;
		} catch (RuntimeException re) {
			LOG.error("get failed", re);
			throw re;
		}
	}

	public List<?> findByExample(Zone instance) {
		LOG.debug("finding Zone instance by example");
		try {
			List<?> results = getSession().createCriteria("com.wheat.vipcard.bean.Zone").add(Example.create(instance))
					.list();
			LOG.debug("find by example successful, result size: " + results.size());
			return results;
		} catch (RuntimeException re) {
			LOG.error("find by example failed", re);
			throw re;
		}
	}

	public List<?> findByProperty(String propertyName, Object value) {
		LOG.debug("finding Zone instance with property: " + propertyName + ", value: " + value);
		try {
			String queryString = "from Zone as model where model." + propertyName + "= ?";
			Query queryObject = getSession().createQuery(queryString);
			queryObject.setParameter(0, value);
			return queryObject.list();
		} catch (RuntimeException re) {
			LOG.error("find by property name failed", re);
			throw re;
		}
	}

	public List<?> findByZoneCode(Object zoneCode) {
		return findByProperty(ZONE_CODE, zoneCode);
	}

	public List<?> findByZoneName(Object zoneName) {
		return findByProperty(ZONE_NAME, zoneName);
	}

	public List<?> findByZoneOfCode(Object zoneOfCode) {
		return findByProperty(ZONE_OF_CODE, zoneOfCode);
	}

	public List<?> findAll() {
		LOG.debug("finding all Zone instances");
		try {
			String queryString = "from Zone";
			Query queryObject = getSession().createQuery(queryString);
			return queryObject.list();
		} catch (RuntimeException re) {
			LOG.error("find all failed", re);
			throw re;
		}
	}

	/**
	 * 分页查询
	 * 
	 * @param zone
	 * @param pageNo
	 * @param pageSize
	 * @return
	 */
	public PageResultSet<Zone> findZoneByPage(Zone zone, int pageNo, int pageSize) {
		String queryHQL = bulidSQL(zone);
		int totalRow = queryCount(queryHQL);
		PageUtil pages = new PageUtil(totalRow, pageSize, pageNo);
		List<Object[]> list = queryList(queryHQL, pageNo, pageSize);
		List<Zone> zList = new ArrayList<Zone>();
		for (int i = 0, len = list.size(); i < len; i++) {
			Zone z = new Zone();
			z.setId(list.get(i)[0] == null ? 0 : Integer.parseInt(list.get(i)[0].toString()));
			z.setZoneCode(list.get(i)[1] == null ? "" : list.get(i)[1].toString());
			z.setZoneName(list.get(i)[2] == null ? "" : list.get(i)[2].toString());
			z.setZoneOfCode(list.get(i)[3] == null ? "" : list.get(i)[3].toString());
			zList.add(z);
		}
		PageResultSet<Zone> page = new PageResultSet<Zone>();
		page.setList(zList);
		page.setPage(pages);
		return page;
	}

	public Zone merge(Zone detachedInstance) {
		LOG.debug("merging Zone instance");
		try {
			Zone result = (Zone) getSession().merge(detachedInstance);
			LOG.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			LOG.error("merge failed", re);
			throw re;
		}
	}

	public void attachDirty(Zone instance) {
		LOG.debug("attaching dirty Zone instance");
		try {
			getSession().saveOrUpdate(instance);
			LOG.debug("attach successful");
		} catch (RuntimeException re) {
			LOG.error("attach failed", re);
			throw re;
		}
	}

	public void attachClean(Zone instance) {
		LOG.debug("attaching clean Zone instance");
		try {
			getSession().lock(instance, LockMode.NONE);
			LOG.debug("attach successful");
		} catch (RuntimeException re) {
			LOG.error("attach failed", re);
			throw re;
		}
	}

	/**
	 * 构建sql
	 * 
	 * @param zone
	 * @return
	 */
	public String bulidSQL(Zone zone) {
		String sql = "select * from zone z where  1=1";
		if (zone != null) {
			if (StringUtils.isNotEmpty(zone.getZoneName())) {
				sql += " and z.zoneName='" + zone.getZoneName() + "'";
			}
			if (StringUtils.isNotBlank(zone.getZoneOfCode())) {
				sql += " and z.zoneOfCode='" + zone.getZoneOfCode() + "'";
			}
		}
		return sql;
	}
}